﻿using Ekton_Manage_DomainCore;
using Ekton_Manage_InfrastructureCore.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ekton_Manage_Web.Extensions.ServiceCollectionExtensions
{
    public static class DbContextSetup
    {
        private static readonly object SingLock = new object();
        public static IServiceCollection AddUseContext(this IServiceCollection services)
        {
            return services.AddDomainContext(async builder =>
            {
                switch ((await Appsettings.GetConnectionObject()).Item1)
                {
                    case "MySql":
                            //ServerVersion serverVersion = ServerVersion.AutoDetect((await Appsettings.GetConnectionObject()).Item2);
                            //builder.UseMySql((await Appsettings.GetConnectionObject()).Item2, serverVersion);
                            builder.UseMySql((await Appsettings.GetConnectionObject()).Item2);
                        break;
                    case "SqlServer":
                        builder.UseSqlServer((await Appsettings.GetConnectionObject()).Item2);
                        break;
                    case "PostgreSql":
                        builder.UseNpgsql((await Appsettings.GetConnectionObject()).Item2);
                        break;
                    case "Sqlite":
                        builder.UseSqlite((await Appsettings.GetConnectionObject()).Item2);
                        break;
                    default:
                            //serverVersion = ServerVersion.AutoDetect((await Appsettings.GetConnectionObject()).Item2);
                            builder.UseMySql((await Appsettings.GetConnectionObject()).Item2);
                        break;
                }
            });
        }

        public static IServiceCollection AddDomainContext(this IServiceCollection services, Action<DbContextOptionsBuilder> optionsAction)
        {
            return services.AddDbContext<ApplicationDbContent>(optionsAction);
        }
    }
}
